<template>
  <view v-if="goodsinfo.goods_name" class="goods-detail">
    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="2000" :duration="1000" :circular="true">
      <swiper-item v-for="(item,i) in goodsinfo.pics" :key="i" @click="preview(i)">
        <image :src="item.pics_big"></image>
      </swiper-item>
    </swiper>
    <!-- 商品信息区域 -->
    <view class="goods-info-box">
      <!-- 商品价格 -->
      <view class="goods-price">
        ￥{{goodsinfo.goods_price==0?99:goodsinfo.goods_price}}
      </view>
      <!-- 商品信息主体 -->
      <view class="goods-main">
        <!-- 商品名称 -->
        <view class="goods-name">
          {{goodsinfo.goods_name}}
        </view>
        <!-- 收藏 -->
        <view class="goods-star" v-if="isshow" @click="starGoods">
          <uni-icons color="red"  type="star-filled" size="25"></uni-icons>
          <text>已收藏</text>
        </view>
        <view v-if="!isshow" class="goods-star" @click="starGoods">
          <uni-icons  type="star" size="25"></uni-icons>
          <text>点击收藏</text>
        </view>
      </view>
      <!-- 免运费 -->
      <view class="yf">
        快递：免运费
      </view>
    </view>
    <!-- 商品详情信息 -->
    <rich-text :nodes="goodsinfo.goods_introduce"></rich-text>
    <!-- 下方导航区 -->
    <view class="goods-nav">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from "vuex"
  export default {
    watch: {
      total: {
        handler(newval) {
          this.options[1].info = newval
        },
        immediate: true
      }
    },
    computed: {
      ...mapState('m_cart', ['cart']),
      ...mapState('m_star', ['star']),
      ...mapGetters('m_cart', ['total']),
      isshow(){
        return this.star.some(x=>x.goods_id==this.goods_id)
      }
    },
    data() {
      return {
        goodsinfo: [],
        goods_id: '',
        options: [{
          icon: 'shop',
          text: '店铺',
          infoBackgroundColor: '#007aff',
          infoColor: "red"
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]
      };
    },
    onLoad(options) {
      this.goods_id = options.goods_id
      this.getGoodsInfo()
    },
    methods: {
      ...mapMutations('m_cart', ['addToCart']),
      ...mapMutations('m_star', ['addToSart']),
      starGoods() {
        const goods = {
          goods_id: this.goods_id,
          goods_name: this.goodsinfo.goods_name,
          goods_price: this.goodsinfo.goods_price,
          goods_small_logo: this.goodsinfo.goods_small_logo
        }
        this.addToSart(goods)
      },
      async getGoodsInfo() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id: this.goods_id
        })
        if (res.meta.status !== 200) return uni.$showMsg()
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ')
          .replace(/webp/g, 'jpg')
        this.goodsinfo = res.message
      },
      preview(i) {
        uni.previewImage({
          current: i,
          urls: this.goodsinfo.pics.map(x => x.pics_big)
        })
      },
      onClick(e) {
        if (e.content.text == '购物车') {
          uni.switchTab({
            url: "/pages/cart/cart"
          })
        }
      },
      buttonClick(e) {
        if (e.content.text == '加入购物车') {
          const goods = {
            goods_id: this.goods_id,
            goods_name: this.goodsinfo.goods_name,
            goods_price: this.goodsinfo.goods_price,
            goods_count: 1,
            goods_small_logo: this.goodsinfo.goods_small_logo,
            goods_state: true,
          }
          this.addToCart(goods)
          // this.options[1].info = this.cart.length
        }
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 750rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }

  .goods-info-box {
    // border-top: 1px solid #eee;
    background-color: #fff;
    padding: 10px;
    padding-right: 0;

    .goods-price {
      color: red;
      font-size: 18px;
      margin: 10px 0;
    }

    .goods-main {
      display: flex;
      justify-content: space-between;

      .goods-name {
        font-size: 13px;
        margin-right: 10px;
      }

      .goods-star {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        border-left: 1px solid #eee;
        width: 120px;
        font-size: 12px;
        color: #848484;
      }
    }

    .yf {
      font-size: 12px;
      color: gray;
      margin: 10px 0;
    }
  }

  .goods-nav {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
  }

  .goods-detail {
    padding-bottom: 50px;
  }
</style>
